from typing import Optional, List

from fastapi import APIRouter, Depends, Query, Body
from sqlalchemy.orm import Session

from server.app.cloud_ide.model import IdeImage
from server.app.cloud_ide.schema.ide_image.request.ide_image_create_request import IdeImageCreateRequest
from server.app.cloud_ide.schema.ide_image.request.ide_image_update_request import IdeImageUpdateRequest
from server.app.cloud_ide.service.ide_image_service import IdeImageService
from server.framework.core.http_schemas import CommonResponseSchema, TableResponseSchema
from server.framework.core.injects import get_db
from server.framework.core.schemas import OptionsSchema

ide_image_api = APIRouter()
ide_image_service = IdeImageService()


@ide_image_api.get("/get_ide_image_options", name='获取镜像下拉选项列表', response_model=List[OptionsSchema])
async def get_ide_image_options(registry_id: str = Query(None, description='镜像源id'),
                                db: Session = Depends(get_db)) -> List[OptionsSchema]:
    results = ide_image_service.get_ide_images_by_registry_id(db, registry_id)

    option_results: List[OptionsSchema] = []
    for row in results:
        option_results.append(OptionsSchema(value=str(row.id), label=row.name))
    return option_results


@ide_image_api.get("/get_entity", name='获取镜像')
async def get_entity(id: str = Query(None, description='镜像id'), db: Session = Depends(get_db)) -> IdeImage:
    entity = ide_image_service.get_ide_image_by_id(db, id)
    return entity


@ide_image_api.get("/list", response_model=TableResponseSchema, name='获取镜像列表')
async def list(name: Optional[str] = Query(None, description='过滤条件:镜像名称'),
               ide_registry_id: Optional[str] = Query(None, description='过滤条件:镜像源id'),
               current: Optional[int] = Query(1, description='当前页数'),
               page_size: Optional[int] = Query(10, description='每页记录的数量', alias='pageSize'),
               db: Session = Depends(get_db)) -> TableResponseSchema:
    """
    获取IDE镜像列表
    :return:
    """
    items, total = ide_image_service.get_ide_image_list(db, ide_registry_id, name, page_size, current)
    return TableResponseSchema(data=items, total=total, page=page_size, success=True)


@ide_image_api.post("/create", response_model=CommonResponseSchema, name='创建镜像')
async def create(data: IdeImageCreateRequest = Body(None, description='镜像创建请求'),
                 db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    创建IDE镜像配置
    :return:
    """
    rs = ide_image_service.create_ide_image(db, data)
    if rs:
        return CommonResponseSchema(data={}, message=f'创建成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'创建失败', success=False)


@ide_image_api.get("/delete", response_model=CommonResponseSchema, name='删除镜像')
async def delete(ids: str = Query(None, description='镜像id，多条记录以逗号分割'),
                 db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    删除IDE镜像配置
    :return:
    """
    rs = ide_image_service.delete_ide_image(db, ids.split(','))
    if rs:
        return CommonResponseSchema(data={}, message=f'删除成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'删除失败', success=False)


@ide_image_api.post("/edit", response_model=CommonResponseSchema, name='编辑镜像')
async def edit(data: IdeImageUpdateRequest = Body(None, description='镜像编辑实体'),
               db: Session = Depends(get_db)) -> CommonResponseSchema:
    """
    编辑IDE镜像配置
    :return:
    """
    rs = ide_image_service.update_ide_image(db, data)
    if rs:
        return CommonResponseSchema(data={}, message=f'编辑成功', success=True)
    else:
        return CommonResponseSchema(data={}, message=f'编辑失败', success=False)


@ide_image_api.get("/show_logs", name='构建镜像', response_model=CommonResponseSchema)
async def show_logs(id: str = Query(None, description='镜像ID'),
                    db: Session = Depends(get_db)):
    result = ide_image_service.get_ide_build_log(db, id)
    return CommonResponseSchema(data=result, message='')


@ide_image_api.get("/build", name='构建镜像', response_model=CommonResponseSchema)
async def build(id: str = Query(None, description='镜像ID'),
                db: Session = Depends(get_db)):
    rs = ide_image_service.build_ide_image(db, id)
    if rs:
        return CommonResponseSchema(message='构建成功', data={}, success=True)
    else:
        return CommonResponseSchema(message='构建失败', data={}, success=False)
